package com.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期相关的操作
 */

public class DateUtil {

    /**
     * 将一个字符串转换成日期格式
     * 
     * @param date
     * @param pattern
     * @return
     */
    public static Date toDate(String date, String pattern) {
        if (("" + date).equals("")) {
            return null;
        }
        if (pattern == null) {
            pattern = "yyyy-MM-dd";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        Date newDate = new Date();
        try {
            newDate = sdf.parse(date);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return newDate;
    }

    /**
     * 把日期转换成字符串型
     * 
     * @param date
     * @param pattern
     * @return
     */
    public static String toString(Date date, String pattern) {
        if (date == null) {
            return "";
        }
        if (pattern == null) {
            pattern = "yyyy-MM-dd";
        }
        String dateString = "";
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        try {
            dateString = sdf.format(date);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return dateString;
    }

    public static String toString(Long time, String pattern) {
        if (time > 0) {
            if (time.toString().length() == 10) {
                time = time * 1000;
            }
            Date date = new Date(time);
            String str = DateUtil.toString(date, pattern);
            return str;
        }
        return "";
    }

    /**
     * 获取上个月的开始结束时间
     * 
     * @return
     */
    public static String[] getLastMonth() {
        // 取得系统当前时间
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH) + 1;

        // 取得系统当前时间所在月第一天时间对象
        cal.set(Calendar.DAY_OF_MONTH, 1);

        // 日期减一,取得上月最后一天时间对象
        cal.add(Calendar.DAY_OF_MONTH, -1);

        // 输出上月最后一天日期
        int day = cal.get(Calendar.DAY_OF_MONTH);

        String months = "";
        String days = "";

        if (month > 1) {
            month--;
        } else {
            year--;
            month = 12;
        }
        if (!(String.valueOf(month).length() > 1)) {
            months = "0" + month;
        } else {
            months = String.valueOf(month);
        }
        if (!(String.valueOf(day).length() > 1)) {
            days = "0" + day;
        } else {
            days = String.valueOf(day);
        }
        String firstDay = "" + year + "-" + months + "-01";
        String lastDay = "" + year + "-" + months + "-" + days;

        String[] lastMonth = new String[2];
        lastMonth[0] = firstDay;
        lastMonth[1] = lastDay;

        // System.out.println(lastMonth[0] + "||" + lastMonth[1]);
        return lastMonth;
    }

    /**
     * 获取当月的开始结束时间
     * 
     * @return
     */
    public static String[] getCurrentMonth() {
        // 取得系统当前时间
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH) + 1;

        // 取得系统当前时间所在月第一天时间对象
        cal.set(Calendar.DAY_OF_MONTH, 1);

        // 日期减一,取得上月最后一天时间对象
        cal.add(Calendar.DAY_OF_MONTH, -1);

        // 输出上月最后一天日期
        int day = cal.get(Calendar.DAY_OF_MONTH);

        String months = "";
        String days = "";

        if (!(String.valueOf(month).length() > 1)) {
            months = "0" + month;
        } else {
            months = String.valueOf(month);
        }
        if (!(String.valueOf(day).length() > 1)) {
            days = "0" + day;
        } else {
            days = String.valueOf(day);
        }
        String firstDay = "" + year + "-" + months + "-01";
        String lastDay = "" + year + "-" + months + "-" + days;

        String[] currentMonth = new String[2];
        currentMonth[0] = firstDay;
        currentMonth[1] = lastDay;

        // System.out.println(lastMonth[0] + "||" + lastMonth[1]);
        return currentMonth;
    }

    /**
     * @author zt_sh
     * @description getDateline:获取当前时间的int值
     * @return
     */
    public static int getDateline() {
        return (int) (System.currentTimeMillis() / 1000);
    }

    /**
     * @author zt_sh
     * @description getDatelineLong:获取当前时间long值
     * @return
     */
    public static long getDatelineLong() {
        return System.currentTimeMillis() / 1000;
    }

    /**
     * @author zt_sh
     * @description getDateline:获取指定时间int值
     * @param date
     * @return
     */
    public static int getDateline(String date) {
        return (int) (toDate(date, "yyyy-MM-dd").getTime() / 1000);
    }

    /**
     * @author zt_sh
     * @description getDateline:获取指定时间long值
     * @param date
     * @param pattern
     * @return
     */
    public static long getDatelineLong(String date) {
        return (long) (toDate(date, "yyyy-MM-dd").getTime() / 1000);
    }

    /**
     * @author zt_sh
     * @description getDateline:指定格式指定时间获取int值
     * @param date
     * @param pattern
     * @return
     */
    public static int getDateline(String date, String pattern) {
        return (int) (toDate(date, pattern).getTime() / 1000);
    }

    /**
     * @author zt_sh
     * @description getDateline:指定格式指定时间获取long值
     * @param date
     * @param pattern
     * @return
     */
    public static long getDatelineLong(String date, String pattern) {
        return (long) (toDate(date, pattern).getTime() / 1000);
    }

    /**
     * @author   zt_sh
     * @description  differentDays: 取两个时间差绝对值  毫秒
     * @param date1 
     * @param date2
     * @return
     */
    public static int differentDays(Date date1, Date date2) {
        return Math.abs((int) (date1.getTime() - date2.getTime()));
    }
    
    public static void main(String[] args) {
        System.out.println(differentDays(toDate("2018-07-17 14:52:19", "yyyy-MM-dd HH:mm:ss"), toDate("2018-07-18 14:32:20", "yyyy-MM-dd HH:mm:ss")));
        System.out.println(85201000 / 1000 / 60);
    }
}
